package com.zh.note.leetcode.backtracking;

import java.util.ArrayList;
import java.util.List;

/**
 * 17. 电话号码的字母组合
 */
public class LC_17_letterCombinations {

    public List<String> letterCombinations(String digits) {
        List<String> res = new ArrayList<>();
        StringBuilder path = new StringBuilder();
        if (digits == null || digits.length() == 0) {
            return res;
        }
        String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        //迭代处理
        dfs(digits, numString, 0, path, res);
        return res;
    }

    private void dfs(String digits, String[] numString, int index, StringBuilder path, List<String> res) {
        if (index == digits.length()) {
            res.add(path.toString());
            return;
        }
        int digit = digits.charAt(index) - '0';// 将index指向的数字转为int
        String str = numString[digit];// 取数字对应的字符集
        for (int i = 0; i < str.length(); i++) {
            path.append(str.charAt(i));
            dfs(digits, numString, index + 1, path, res);
            path.deleteCharAt(path.length() - 1);
        }
    }
}
